//
//  DGCirculardiagramView.m
//
//
//  Created by yidong on 16/11/1.
//  Copyright © 2016年 东哥. All rights reserved.
//

#import "DGCirculardiagramView.h"

#define Kduring 0.6f

@interface DGCirculardiagramView()<CAAnimationDelegate>

@end

@implementation DGCirculardiagramView
{
    float width;
    
    float height;
    
    float deslineWidth;
    
    float margin;
}
#pragma mark - 绘制图形
-(void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    
    deslineWidth = 110.0;
    margin = 15.5;
    
    width = self.frame.size.width;
    height = self.frame.size.height;
    
    CGPoint center = CGPointMake(width/2, height/2);
    
    CGFloat radius = (height-80.0)/2.0;
    
    __block CGFloat startA = 0;
    __block CGFloat angle = 0;
    __block CGFloat endA = -1.6*M_PI;
    __block UIColor *setcolor;
    
    NSMutableArray *points = [NSMutableArray array];
    
    [_ratioArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        
        float value = [obj floatValue];
        
        CAShapeLayer *layer = [CAShapeLayer layer] ;
        UIBezierPath *path = [UIBezierPath bezierPath];
        
        layer.fillColor = [UIColor clearColor].CGColor;
        
        startA = endA;
        angle = value * M_PI * 2;
        endA = startA + angle;
        
        
        if (idx < _colorArray.count) {
            setcolor = [_colorArray objectAtIndex:idx];
        }else{
            setcolor = [UIColor orangeColor];
        }

        layer.strokeColor = setcolor.CGColor;
        
        //clockwise：YES为顺时针，No为逆时针 startA endA 前加负号
        [path addArcWithCenter:center radius:radius startAngle:-startA  endAngle:-endA clockwise:NO];
        
        layer.path = path.CGPath;
        [self.layer addSublayer:layer];
        layer.lineWidth = radius-margin;
        CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
        basic.fromValue = @(0);
        basic.toValue = @(1);
        basic.duration = Kduring;
        basic.fillMode = kCAFillModeForwards;
        
        [layer addAnimation:basic forKey:@"basic"];
        
        /**
          *  求出一段扇形弧上的中点坐标(圆的参数方程)
         */
        
        
        CGPoint piePoint;
        
        piePoint.x = center.x+(radius+margin+5)*cos(-(startA+endA)/2);
        
        piePoint.y = center.y+(radius+margin+5)*sin(-(startA+endA)/2);
        
        NSLog(@"piePoint=====%@",NSStringFromCGPoint(piePoint));
        

        [points addObject:[NSValue valueWithCGPoint:piePoint]];
        
    }];
    
    [self drawWithPoints:points];
    
}
#pragma mark - 根据点在象限里绘画描述文字
-(void)drawWithPoints:(NSMutableArray *)points
{
    
    
    CGPoint center = CGPointMake(width/2, height/2);
    
    [points enumerateObjectsUsingBlock:^(NSValue *pointValue, NSUInteger idx, BOOL * _Nonnull stop) {
       
        CGPoint piePoint = [pointValue CGPointValue];
        
        UIColor *color = _colorArray[idx];
        NSDictionary *dict = _descriptionArray[idx];
        NSString *num = dict[@"num"];
        NSString *des = dict[@"des"];
        
        NSDictionary *numattrs =@{NSForegroundColorAttributeName:color,NSFontAttributeName:[UIFont systemFontOfSize:15]};
        
        NSDictionary *desattrs =@{NSForegroundColorAttributeName:[UIColor colorWithRed:190.0/255.0 green:190.0/255.0 blue:190.0/255.0 alpha:1],NSFontAttributeName:[UIFont systemFontOfSize:15]};
        
        //如果为0就不显示
        if ([num intValue]>0) {
            
            UIBezierPath *path = [[UIBezierPath alloc] init];
            path.lineWidth = 1;
    
            CAShapeLayer *layer = [CAShapeLayer layer] ;
            layer.fillColor = [UIColor clearColor].CGColor;
            layer.strokeColor = color.CGColor;

            
            //第一象限 第二象限
            if (piePoint.x<=center.x) {
                [path moveToPoint:CGPointMake(piePoint.x-10, piePoint.y)];
                
                [path addLineToPoint:CGPointMake(piePoint.x-deslineWidth, piePoint.y)];
                
                [num drawAtPoint:CGPointMake(piePoint.x-deslineWidth, piePoint.y-20) withAttributes:numattrs];
                
                [des drawAtPoint:CGPointMake(piePoint.x-deslineWidth, piePoint.y+3) withAttributes:desattrs];
                
            }
            
            //第三象限 第四象限
            if (piePoint.x>center.x) {
                
                [path moveToPoint:CGPointMake(piePoint.x+10, piePoint.y)];
                
                [path addLineToPoint:CGPointMake(piePoint.x+deslineWidth, piePoint.y)];

                
                [num drawAtPoint:CGPointMake(piePoint.x+deslineWidth-30, piePoint.y-20) withAttributes:numattrs];
                    
                [des drawAtPoint:CGPointMake(piePoint.x+deslineWidth-65, piePoint.y+3) withAttributes:desattrs];
               
            }
            
            layer.path = path.CGPath;
            [self.layer addSublayer:layer];
            CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
            basic.fromValue = @(0);
            basic.toValue = @(1);
            basic.duration = Kduring;
            basic.fillMode = kCAFillModeForwards;
            
            [layer addAnimation:basic forKey:@"basic"];
            
        }
        
        
    }];
    
}

-(void)setRatioArray:(NSMutableArray *)ratioArray
{
    _ratioArray = ratioArray;
    
    [self setNeedsDisplay];
    
}

@end
